//
//  LMJLiangPinController.m
//  EPA
//
//  Created by lanou3g on 16/4/21.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "LMJLiangPinController.h"
#import "LMJNetWorkRequestManager.h"
#import "URLHeaderDefine.h"
#import "LMJLiangPinModel.h"
#import "LMJLiangPinCell.h"
#import "LORefresh.h"
@interface LMJLiangPinController ()
@property (nonatomic,strong) NSMutableArray *liangPinArray;
@property (nonatomic,strong) NSString *start;
@property (nonatomic,strong) LORefresh *headerRefresh;
@end

@implementation LMJLiangPinController
-(void)dealloc {
    [self.tableView removeObserver:[self.tableView gifHeader] forKeyPath:@"contentOffset"];
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentOffset"];
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentSize"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.title = @"PRODUCTS";
    
    //注册
    [self.tableView  registerNib:[UINib nibWithNibName:@"LMJLiangPinCell" bundle:nil] forCellReuseIdentifier:@"liangpinCell"];
       self.start = @"0";
    //调用
    [self loadDateFromServerStart:self.start];
    //刷新
 
    __weak typeof(self) weakSelf = self;
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderGif refreshingBlock:^{
        //移除
        [weakSelf.liangPinArray removeAllObjects];
        [weakSelf.tableView reloadData];
        [weakSelf loadDateFromServerStart:@"0"];
    }];
    self.headerRefresh = [self.tableView gifHeader];
    [self.headerRefresh setGifName:@"wangnima"];
    //上提
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        NSInteger count = weakSelf.liangPinArray.count;
        weakSelf.start = [NSString stringWithFormat:@"%ld",count];
        //网络请求
        [weakSelf loadDateFromServerStart:weakSelf.start];
    }];
    
   }
#pragma mark -- LiangPin刷新
-(void)loadDateFromServerStart:(NSString *)start {
    
    //
    self.liangPinArray = [NSMutableArray new];
    [LMJNetWorkRequestManager requestURL:[NSURL URLWithString:SHOPLIST_URL] Method:LMJ_POST Params:@{@"start":start,@"client":@"1",@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"limit":@"10",@"auth":@"",@"version":@"3.0.2"} Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        //解析数据
        if (data) {
    
        NSMutableDictionary  *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dataDic = jsonDic[@"data"];
        //        NSLog(@"******%@",dataDic);
        NSArray *listDic = dataDic[@"list"];
        //        NSLog(@"******%@",listDic);
        for (NSDictionary *itemDic  in listDic) {
            LMJLiangPinModel *lpModel = [LMJLiangPinModel new];
            [lpModel setValuesForKeysWithDictionary:itemDic];
            [self.liangPinArray addObject:lpModel];
            NSLog(@"**********%@",self.liangPinArray);
            
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            //手动停止刷新/加载
            [[self.tableView gifHeader]endRefreshing];
            [[self.tableView defaultFooter]endRefreshing];
        });
        }
    }];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.liangPinArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LMJLiangPinCell *cell = [tableView dequeueReusableCellWithIdentifier:@"liangpinCell" forIndexPath:indexPath];
    
    // Configure the cell...
    cell.model = self.liangPinArray[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 127;
}

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
